﻿Imports PTIC_GestionBDD
Imports PTIC_Util

Public Class Segmentation
    Inherits baseObservable
    Implements IObserver

#Region "Variables de classe"
    Protected _lVarSegmentationPossNonRetenue As New Dictionary(Of String, Correspondance)
    Protected _lVarSegmentationRetenue As New Dictionary(Of String, Correspondance)
    Protected _universOMANE As UniversOMANE
#End Region

#Region "Création d'une instance"
    Public Sub New(ByVal univers As UniversOMANE)
        _lVarSegmentationPossNonRetenue = New Dictionary(Of String, Correspondance)
        _lVarSegmentationRetenue = New Dictionary(Of String, Correspondance)
        _universOMANE = univers
    End Sub
#End Region

#Region "Gestion des variables de segmentation"

    ''' <summary>
    ''' Modifie la liste des correspondances non retenues mais qu'il est possibles de retenir en segmentation
    ''' </summary>
    ''' <param name="listeSegments">Liste de correspondances à ajouter/supprimer</param>
    ''' <param name="operation">Opération à effectuer</param>
    ''' <remarks></remarks>
    Private Sub modifSegPossibleNonRetenue(ByVal listeSegments As List(Of Correspondance), _
                                           ByVal operation As operationCollection)

        If operation = operationCollection.REPLACE Then
            _lVarSegmentationPossNonRetenue = New Dictionary(Of String, Correspondance)
        End If

        If operation = operationCollection.REPLACE Or operation = operationCollection.ADD Then
            For Each corresp As Correspondance In listeSegments
                _lVarSegmentationPossNonRetenue.Add(corresp.description, corresp)
            Next
        ElseIf operation = operationCollection.REMOVE Then
            For Each corresp As Correspondance In listeSegments
                If _lVarSegmentationPossNonRetenue.ContainsKey(corresp.description) Then
                    _lVarSegmentationPossNonRetenue.Remove(corresp.description)
                End If
            Next
        End If
    End Sub

    ''' <summary>
    ''' Modifie la liste des correspondances retenues en segmentation
    ''' </summary>
    ''' <param name="listeSegments">Liste de correspondances à ajouter/supprimer</param>
    ''' <param name="operation">Opération à effectuer</param>
    ''' <remarks></remarks>
    Public Sub modifSegRetenue(ByVal listeSegments As List(Of Correspondance), _
                               ByVal operation As operationCollection)

        'Lorsque la segmentation retenue est modifiée, le traitement permettant d'extraire la segmentation
        'est modifiée également
        Dim reqSegmentation As RequeteBDD = _universOMANE.listeTraitements.getTraitReqSegmentation()

        If operation = operationCollection.REPLACE Then
            'Mise à jour de la collection
            _lVarSegmentationRetenue = New Dictionary(Of String, Correspondance)
            For Each corresp As Correspondance In reqSegmentation.ListeChampsSortie.Values
                'Mise à jour du traitement + refaire la jointure avec le traitement suivant
                reqSegmentation.removeSELECT(corresp.description)
            Next
        End If

        If operation = operationCollection.ADD Or operation = operationCollection.REPLACE Then
            For Each corresp As Correspondance In listeSegments
                'Mise à jour de la collection
                _lVarSegmentationRetenue.Add(corresp.description, corresp)
                'Mise à jour du traitement
                reqSegmentation.addSELECT(corresp.traitement.nom, corresp.description)
            Next
        ElseIf operation = operationCollection.REMOVE Then
            For Each corresp As Correspondance In listeSegments
                If _lVarSegmentationRetenue.ContainsKey(corresp.description) Then
                    'Mise à jour de la collection
                    _lVarSegmentationRetenue.Remove(corresp.description)
                    'Mise à jour du traitement
                    reqSegmentation.removeSELECT(corresp.description)
                End If
            Next
        End If

        notifieChangement(Me, New With {.notification = notification.ModifSegmentsRetenus, _
                                       .operation = operation, _
                                       .listeSegments = listeSegments})

    End Sub

    ''' <summary>
    ''' Renvoie le dictionnaire des correspondances utilisables mais non utilisées en segmentation
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PossibleNonRetenue() As Dictionary(Of String, Correspondance)
        Return _lVarSegmentationPossNonRetenue
    End Function

    ''' <summary>
    ''' Renvoie le dictionnaire des correspondances utilisées en segmentation
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Retenue() As Dictionary(Of String, Correspondance)
        Return _lVarSegmentationRetenue
    End Function

#End Region

#Region "Actualisations liées aux observations du modèle"

    ''' <summary>
    ''' Redirection des observations
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="notification"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function actualise(ByVal sender As Object, ByVal notification As Object) As PTIC_Util.resultatValidationContraintes Implements PTIC_Util.IObserver.actualise

        'Décision en fonction du type de l'objet observé
        If TypeOf sender Is Correspondance Then

            'Si la correspondance modifiée ayant généré la modification provient du traitement préparatoire 1 de la table d'encours
            Dim correspModifiee As Correspondance = DirectCast(sender, Correspondance)

            If correspModifiee.traitement.nom = PTIC_Util.nomTraitTbleEncoursPrep1 Then
                Dim notifModifCorrespondance As Correspondance.Notification = DirectCast(notification, Correspondance.Notification)

                'L'état d'une correspondance du traitement préparatoire N°1 de la table d'encours a changé
                If notifModifCorrespondance = Correspondance.Notification.EtatAchange Then
                    actualiseFromEtatCorrespPrepEncoursHasChanged(correspModifiee)
                End If

            End If

        End If
        Return Nothing
    End Function

    ''' <summary>
    ''' Changement d'état d'une correspondance du traitement préparatoire 1 de la table d'encours
    ''' </summary>
    ''' <param name="correspModifiee"></param>
    ''' <remarks></remarks>
    Private Sub actualiseFromEtatCorrespPrepEncoursHasChanged(ByVal correspModifiee As Correspondance)

        'Modification de la liste de segmentation possible et non retenue
        Dim lSegAajouter As New List(Of Correspondance)
        lSegAajouter.Add(correspModifiee)

        Dim notifModifSegmentation As notification, operation As operationCollection

        If correspModifiee.etat = Color.Green Then
            operation = operationCollection.ADD
            notifModifSegmentation = notification.AjoutSegmentsPoss
        Else
            operation = operationCollection.REMOVE
            notifModifSegmentation = notification.SuppressionSegmentsPoss
        End If


        modifSegPossibleNonRetenue(lSegAajouter, operation)
        notifieChangement(Me, New With {.notification = notifModifSegmentation, _
                                        .listeSegments = lSegAajouter})

    End Sub

#End Region

#Region "Notification - Objet observable"
    Public Enum notification
        AjoutSegmentsPoss
        SuppressionSegmentsPoss
        ModifSegmentsRetenus
    End Enum
#End Region

End Class
